import { Injectable } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { PrismaService } from 'nestjs-prisma'
import { DomainModel } from '../model'

@Injectable()
export class MetaResourceModuleActionDao {
  get table() {
    return this.prismaService.metaResourceModuleAction
  }

  constructor(private readonly prismaService: PrismaService) {}

  /**
   * 数据列表
   * @param data
   * @returns
   */
  list(data: Prisma.MetaResourceModuleActionWhereInput): Promise<DomainModel.MetaResourceModuleAction[]> {
    return this.table.findMany({
      where: data,
      orderBy: {
        createTime: 'desc',
      },
    })
  }

  /**
   * name是否已经存在
   * @param data
   * @returns
   */
  isNameExists(data: { platform: string; resourceModuleId: number; name: string; id?: number }) {
    return this.table
      .count({
        where: {
          platform: data.platform,
          resourceModuleId: data.resourceModuleId,
          name: data.name,
          id: {
            not: data.id,
          },
        },
      })
      .then(count => count > 0)
  }

  /**
   * label是否已存在
   * @param data
   * @returns
   */
  isLabelExists(data: { platform: string; resourceModuleId: number; label: string; id?: number }) {
    return this.table
      .count({
        where: {
          platform: data.platform,
          resourceModuleId: data.resourceModuleId,
          label: data.label,
          id: {
            not: data.id,
          },
        },
      })
      .then(count => count > 0)
  }

  /**
   * 添加数据
   * @param data
   * @returns
   */
  save(data: Prisma.MetaResourceModuleActionUncheckedCreateInput): Promise<DomainModel.MetaResourceModuleAction> {
    return this.table.create({
      data: {
        platform: data.platform,
        project: data.project,
        resourceModuleName: data.resourceModuleName,
        resourceModuleId: data.resourceModuleId,
        actionType: data.actionType,
        name: data.name,
        label: data.label,
      },
    })
  }

  /**
   * 根据ID更新数据
   * @param id
   * @param data
   * @returns
   */
  updateById(
    id: number,
    data: Prisma.MetaResourceModuleActionUpdateInput
  ): Promise<DomainModel.MetaResourceModuleAction> {
    return this.table.update({
      data: {
        name: data.name,
        label: data.label,
      },
      where: { id },
    })
  }

  /**
   * 根据ID删除数据
   * @param id
   * @param deleteModel
   * @returns
   */
  async deleteById(id: number, deleteModel: boolean): Promise<void> {
    const deletePromises: Prisma.PrismaPromise<any>[] = [this.table.delete({ where: { id } })]

    if (deleteModel) {
      deletePromises.push(
        this.prismaService.metaResourceModel.deleteMany({
          where: {
            relativeType: DomainModel.MetaResourceRelativeType.ModuleAction,
            relativeId: id,
          },
        })
      )
    }

    await this.prismaService.$transaction(deletePromises)
  }

  /**
   * 根据ID获取数据
   * @param id
   * @returns
   */
  getById(id: number): Promise<DomainModel.MetaResourceModuleAction> {
    return this.table.findUnique({
      where: {
        id,
      },
    })
  }
}
